package com.cskaoyan.javase.practice.stage5.entity;

import java.util.Arrays;

/**
 * @program: little-practice
 * @description: student list object and store students
 * @author: Keyu Li
 * @create: 2021-09-06 21:08
 **/

public class StudentList {
    private final static int DEFAULT_CAPACITY = 10;
    private Student[] stul;
    private int stuNum;


    public StudentList() {
        this.stul = new Student[DEFAULT_CAPACITY];
        this.stuNum = 0;
    }

    public StudentList(int capacity) {
        this.stul = new Student[capacity];
        this.stuNum = 0;
    }

    public int getStuNum() {
        return stuNum;
    }


    public boolean isEmpty() {
        return stuNum == 0;
    }

    // 扩增学生列表：如果学生列表满了，新建数组为原来2倍长，复制原数组到新数组
    private void expandStul() {
        int len = stul.length;
        Student[] stulNew = new Student[len << 1];
        for (int i = 0; i < len; i++) {
            stulNew[i] = stul[i];
        }
        stul = stulNew;
    }

    // 添加学生：如果学生列表为空，则新建默认长度；如果满了，则扩增数组长度（默认有效）
    public boolean add(Student stu) {
        if (stul == null) stul = new Student[DEFAULT_CAPACITY];
        if (stuNum == stul.length) expandStul();
        for (int i = 0; i < stul.length; i++) {
            if (stul[i] != null) continue;
            stul[i] = stu;
            stuNum++;
            break;
        }
        return true;
    }


    // 查找下标：根据学生id查找并返回下标值，如果没找到则返回-1.
    private int getIndexById(int targetId) {
        if (stul == null || stuNum == 0) return -1;
        for (int i = 0; i < stul.length; i++) {
            if (stul[i] == null) continue;
            if (stul[i].getStuId() == targetId) return i;
        }
        return -1;
    }


    // 删除学生：输入学生id删除学生信息，先确保数组非空，再搜索id返回下标删除。
    public boolean deleteById(int targetId) {
//        if (stul == null || stuNum == 0) return false;
        int index = getIndexById(targetId);
        if (index == -1) return false;
        stul[index] = null;
        stuNum--;
        return true;
    }

    // 更新信息：输入学生id 更新学生信息(默认输入有效stu)
    public boolean updateById(int targetId, Student stu) {
        int index = getIndexById(targetId);
        if (index == -1) return false;
        stul[index] = stu;
        return true;
    }

    // 查找学生：输入学生id，返回查询到的学生对象，否则为null
    public Student findById(int targetId) {
        int index = getIndexById(targetId);
        if (index == -1) return null;
        return stul[index];
    }

    // 返回打印：将学生列表剔除null并返回使控制台打印
    public Student[] printList() {
        if (stul == null || stuNum == 0) return null;
        Student[] printStuList = new Student[stuNum];
        int i = 0;
        for (Student stu : stul) {
            if (stu == null) continue;
            printStuList[i++] = stu;
        }
        return printStuList;
    }

    // 检查id合法：如果输入的id数组内已存在，返回false
    public boolean notExistId(int targetId) {
        if (stul == null || stuNum == 0) return true;
        for (Student stu : stul) {
            if (stu == null) continue;
            if (stu.getStuId() == targetId) return false;
        }
        return true;
    }

    public boolean sortByChoice(int choice){
        if (stul==null||stuNum==0) return false;
        stul = this.printList();
        switch (choice){
            case 1:
                Arrays.sort(stul, Student::sortByAge);
                break;
            case 2:
                Arrays.sort(stul, Student::sortById);
                break;
            case 3:
                Arrays.sort(stul, Student::sortByAgeAndId);
                break;
        }
        return true;
    }

}
